package cn.wolfcode.rbac.service.impl;

import cn.wolfcode.rbac.domain.Permission;
import cn.wolfcode.rbac.mapper.PermissionMapper;
import cn.wolfcode.rbac.query.PageResult;
import cn.wolfcode.rbac.query.QueryObject;
import cn.wolfcode.rbac.service.IPermissionService;
import cn.wolfcode.rbac.util.RequiredPermission;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.applet.AppletContext;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class PermissionServiceImpl implements IPermissionService {
    @Autowired
    private PermissionMapper mapper;

    @Autowired
    private ApplicationContext ctx;

    @Override
    public PageInfo query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Permission> list = this.mapper.selectForList(qo);
        return new PageInfo(list);
    }

    @Override
    public void saveOrUpdate(Permission entity) {
        if(entity.getId()==null){
            this.mapper.insert(entity);
        }else{
            this.mapper.updateByPrimaryKey(entity);
        }
    }

    @Override
    public void delete(Long id) {
        this.mapper.deleteByPrimaryKey(id);
    }

    @Override
    public Permission get(Long id) {
        return this.mapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Permission> list() {
        return this.mapper.selectAll();
    }

    @Override
    public void load() {
        //比较
        //获取permission表中的所有记录
        List<String> permissions = this.mapper.selectAllExpression();
        //通过上下文来获取用户的权限。并保存到集合中。
        Map<String,Object> map =this.ctx.getBeansWithAnnotation(Controller.class);
        Collection<Object> values = map.values();
        for(Object controller : values){
            Class<?> clazz = controller.getClass();
            Method[] methods = clazz.getDeclaredMethods();
            for (Method method:methods) {
                RequiredPermission annotation = (RequiredPermission)method.getAnnotation(RequiredPermission.class);
                if(annotation != null){
                    String name = annotation.name();
                    String simpleName = clazz.getSimpleName().replace("Controller","");

                        //获取该方法中有自定义注解的表达式值
                        String expression = StringUtils.uncapitalize(simpleName)+":"+method.getName();
                        //开始对比，将对比后的信息。保存
                        if(!permissions.contains(expression)){
                            System.out.println("permissions"+permissions);
                            System.out.println("expression"+expression);
                            //保存
                            Permission permission = new Permission();
                            permission.setName(name);
                            permission.setExpression(expression);
                        this.mapper.insert(permission);
                    }
                }
            }
        }

    }

    @Override
    public List<String> selectByEmpId(Long id) {
        return this.mapper.selectByEmpId(id);
    }
}
